const bp = require("photoshop").action.batchPlay
const { DOMParser, XMLSerializer, DOMImplementation } = require('xmlbuilder2');

// Get all the Doc XMP
const getDocumentXMP = () => {
  return bp([{
    _obj: "get",
    _target: {
      _ref: [
        { _property: "XMPMetadataAsUTF8" },
        { _ref: "document", _enum: "ordinal", _value: "targetEnum" }
      ]
    }
  }], { synchronousExecution: true })[0].XMPMetadataAsUTF8
}

// Set all the Doc XMP
const setDocumentXMP = (xmpString) => {
  bp([{
    _obj: "set",
    _target: [
      { _ref: "property", _property: "XMPMetadataAsUTF8" },
      { _ref: "document", _enum: "ordinal", _value: "targetEnum" }
    ],
    to: {
      _obj: "document",
      XMPMetadataAsUTF8: xmpString
    }
  }], {});
}

// Get key of namespace
const getDocumentMetadata = ({ key, namespace }) => {
  const xmpString = getDocumentXMP();
  if (xmpString) {
    const xmlDoc = new DOMParser().parseFromString(xmpString, 'text/xml');
    const node = namespace ? xmlDoc.getElementsByTagNameNS(namespace, key)[0] : xmlDoc.getElementsByTagName(key)[0]
    if (node) {
      return node.textContent
    }
  }
}

// set key, value of namespace
const setDocumentMetadata = ({ key, value, namespace }) => {
  const xmpString = getDocumentXMP();
  const xmlDoc = xmpString ? new DOMParser().parseFromString(xmpString, 'text/xml') : new DOMImplementation().createDocument(namespace, key, null);
  const node = namespace ? xmlDoc.getElementsByTagNameNS(namespace, key)[0] || xmlDoc.createElementNS(namespace, key) :
    xmlDoc.getElementsByTagName(key)[0] || xmlDoc.createElement(key);
  node.textContent = value;
  xmlDoc.appendChild(node);
  console.log(xmlDoc); // the document does contain the new node
  const newXmpString = new XMLSerializer().serializeToString(xmlDoc); // <= something wrong here...?
  console.log("oldXmpString", xmpString);    // Old XMP string
  console.log("newXmpString", newXmpString); // New XMP string... they're equal :-/
  setDocumentXMP(newXmpString)
}

// delete key of namespace
const deleteDocumentMetadata = ({ key, namespace }) => {
  const xmpString = getDocumentXMP();
  const xmlDoc = new DOMParser().parseFromString(xmpString, 'text/xml');
  const node = namespace ? xmlDoc.getElementsByTagNameNS(namespace, key)[0] : xmlDoc.getElementsByTagName(key)[0]
  if (node) {
    xmlDoc.removeChild(node)
    setDocumentXMP(new XMLSerializer().serializeToString(xmlDoc))
  }
}

// TEST
setDocumentMetadata({key: "someKey", value: "someValue", namespace: "undavide:"})
var newXMP = getDocumentMetadata({key: "myKey", namespace: "undavide:"});
console.log({newXMP}) // undefined